package controllers

import (
	"Gin_dome/models"
	"encoding/base64"
	"fmt"
	"github.com/gin-gonic/gin"
	"regexp"
	"strconv"
)

func Details(c *gin.Context) {
	//获取url查询字符串的参数
	//artid := c.DefaultQuery("artid", "MTU4")
	artid := c.Param("artid")
	if len(artid) == 0 {
		artid = "MTU4"
	}
	decodeBytes, _ := base64.StdEncoding.DecodeString(artid)
	strartid := string(decodeBytes[:])

	pattern := "\\d+" //反斜杠要转义
	result, _ := regexp.MatchString(pattern, strartid)

	if result {
		// 代表查询字符串正确
		db := models.Blog{}
		blog, pern, next := db.GetBlog(strartid)
		//标准base64编码

		pern_id := strconv.Itoa(int(pern.ID))

		next_id := strconv.Itoa(int(next.ID))
		// 演示base64编码
		pern_id_base := base64.StdEncoding.EncodeToString([]byte(pern_id))
		next_id_base := base64.StdEncoding.EncodeToString([]byte(next_id))

		pern.ShowImage = pern_id_base
		next.ShowImage = next_id_base

		c.HTML(200, "details.html", gin.H{
			"blog": &blog,
			"pern": &pern,
			"next": &next,
		})
	} else {
		fmt.Println("afewafewafewgregr")
		blog := models.Blog{}
		blog.ID = 0
		c.HTML(200, "details.html", gin.H{
			"blog": blog,
			"pern": nil,
			"next": nil,
		})
	}

}
